package cn.hzking.gjmall.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import org.greenrobot.eventbus.EventBus;

import cn.hzking.gjmall.R;
import cn.hzking.gjmall.activitys.MainActivity;
import cn.hzking.gjmall.activitys.login.RegistActivity;
import cn.hzking.gjmall.bean.WxLoginBean;
import cn.hzking.gjmall.net.RetrofitFactory;
import cn.hzking.gjmall.net.base.BaseObserver;
import cn.hzking.gjmall.net.loading.loadingview.GjLoadingUtils;
import cn.hzking.gjmall.utils.SpPutUtils;
import cn.hzking.gjmall.utils.UIUtils;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by ntop on 15/9/4.
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private String code;//微信获取的code

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WXShare.getInstance(this).handleIntent(getIntent(), this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
    }

    @Override
    public void onResp(BaseResp baseResp) {
        String result = "";
        GjLoadingUtils.stopLoading();
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK://用户同意
                if (baseResp.getType() == 1) {//授权登录成功
                    SendAuth.Resp resp = (SendAuth.Resp) baseResp;
                    code = resp.code;
                    result = "授权成功";
                    Log.e("message", "code=" + code + "");//http://dubaita.mxth.com/
                    login(code);
                } else if (baseResp.getType() == 2) {//微信分享成功
                    //  UIUtils.showToastShort(WXEntryActivity.this,"分享成功");
                } else {
                }
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL://用户取消
                result = "用户取消";
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:// 用户拒绝授权
                result = "权限拒绝";
                Toast.makeText(WXEntryActivity.this, result, Toast.LENGTH_LONG).show();
                break;
            default:
                result = "未知错误";
                Toast.makeText(WXEntryActivity.this, result, Toast.LENGTH_LONG).show();
                break;

        }
        finish();
        /*WXResultBean wx_result = new WXResultBean(code,result, SPUtils.getLoginType(this));
        EventBus.getDefault().post(wx_result);*/
    }



    private void login(String code) {
        RetrofitFactory.getInstant().API()
                .wxLogin(code)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<WxLoginBean>(WXEntryActivity.this, false, false) {
                               @Override
                               protected void onSuccees(WxLoginBean wxLoginBean) throws Exception {
                                   WxLoginBean.DataBean data = wxLoginBean.getData();
                                   if(data.isRequired_bind()){//需要绑定手机号
                                       Intent intent = new Intent(WXEntryActivity.this, RegistActivity.class);
                                       startActivity(intent);
                                   }else {
                                       SpPutUtils.put(WXEntryActivity.this,wxLoginBean);
                                       Intent intent = new Intent(WXEntryActivity.this, MainActivity.class);
                                       startActivity(intent);
                                       EventBus.getDefault().post("close");
                                   }

                               }

                               @Override
                               protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                                   UIUtils.showToast(WXEntryActivity.this,getResources().getString(R.string.request_error),Toast.LENGTH_SHORT);
                               }

                               @Override
                               protected void onCodeError(WxLoginBean wxLoginBean) throws Exception {
                                   super.onCodeError(wxLoginBean);
                                   UIUtils.showToast(WXEntryActivity.this,wxLoginBean.getErrorMsg(),Toast.LENGTH_SHORT);
                               }
                           }
                );
    }


}
